import wx
from pyeasyaccept.gui.AutoComplete import TextCtrlAutoComplete
from pyeasyaccept.gui.CommandsDictionary import CommandsDictionary
from pyeasyaccept.commands.InternalCommands import InternalCommands
from inspect import *

class ScriptLinePanel(wx.Panel):
    """
    It's a panel that represents a line to insert a command for a script
    @author: Mikaela Maia
    """
    
    def __init__(self, parent, id, cmdsDictionary):
         wx.Panel.__init__(self, parent, id, size=wx.Size(500, 300))
         self.label = wx.StaticText(self, -1, 'Command', (0,0))
         self.buttonOK = wx.Button(self, 1, 'OK')
         self.buttonCancel = wx.Button(self, 2, 'Cancel')

         self.Bind(wx.EVT_BUTTON, self.OnOK, id = 1)
         self.Bind(wx.EVT_BUTTON, self.OnCancel, id = 2)
         
         self.grid = wx.GridBagSizer(9, 9)
        
         self._ctrl = TextCtrlAutoComplete(self, cmdsDictionary)
         
         self.grid.Add(self.label, (2,1), (1,1), wx.ALIGN_LEFT)
         self.grid.Add(self._ctrl, (2,2), (1,1), wx.EXPAND)
         self.grid.Add(self.buttonOK, (2,3), (1,1), wx.ALIGN_CENTER)
         self.grid.Add(self.buttonCancel, (2,5), (1,1), wx.ALIGN_CENTER)
         self.SetSizer(self.grid)
         
         self.onBtDynamicChoices(self.onBtDynamicChoices)
         
         self.listeners = []
         
    def getCmdsDictionary(self):
        return self._ctrl.getCmdsDictionary()
    
    def setCmdsDictionary(self, dic):
        self._ctrl.setCmdsDictionary(dic)
        self.resetChoices()
        
    def onBtDynamicChoices(self, event):
        """
        This method sets choices to autocomplete
        @param event: an event  
        """
        self.resetChoices()
        self._ctrl.SetResetListCallback(self.resetChoices)
        
    def resetChoices(self):
        dynamic_choices = self._ctrl.getCmdsDictionary().getDynamicChoices()
        self._ctrl.SetChoices(dynamic_choices)
        self._ctrl.SetEntryCallback(self.setDynamicChoices)
        
    
    def buttonPressed(self, cmd, args):
        """
        This method append the command and his arguments if a button is pressed
        @param cmd: command's name
        @param args: arguments from the command   
        """
        self._ctrl.AppendText(cmd)
        self._ctrl.SetInsertionPointEnd()
        self._ctrl._showDropDown(False)
        self._ctrl.autoComplete(args)
        
     
    def match(self, text, choice):
        """
        This method matches the text and the choice
        """
        t = text.lower()
        c = choice.lower()
        return c.startswith(t)
    
    def OnOK(self,event):
        """
        This method calls addScriptLine from his listeners if the button OK is pressed
        """ 
        for listener in self.listeners:
            listener.addScriptLine(self._ctrl.GetValue())
        self._ctrl.Clear()
    
    def OnCancel(self,event):
        """
        This method clears the textCtrl if the button CANCEL is pressed
        """
        self._ctrl.Clear()
        
    def setDynamicChoices(self):
        """"
        This method sets dynamic choices for the autocomplete
        """
        text = self._ctrl.GetTextToAutoComplete()
        dynamic_choices = self._ctrl.getCmdsDictionary().getDynamicChoices()
        choices = [choice for choice in dynamic_choices if self.match(text, choice)]
        self._ctrl.SetChoices(choices)
        
    def addListener(self, listener):
        """
        This method add a listener for this panel
        """
        self.listeners.append(listener)
    
    def getListeners(self):
        """
        This method return a list of listeners from this panel
        """
        return self.listeners
    
    def reset(self):
        """
        Clears the ScriptLinePanel.
        """
        self._ctrl.Clear()
